package com.ceshiren.chnjx.wework.page.contact;

import com.ceshiren.chnjx.wework.entity.Member;
import com.ceshiren.chnjx.wework.page.BasePage;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * @author JJJJ
 * @Title:
 * @date 2021/11/6下午4:38
 * @Description:
 */
public class ContactPage extends BasePage {
    private final By msgBtn = By.xpath("//*[@text='信息']");
    private final By contactBtn = By.xpath("//*[@text='通讯录']");
    private final By addMemberBtn = By.xpath("//*[@text='添加成员']");
    private final By addManuallyBtn = By.xpath("//*[@text='手动输入添加']");
    private final By backButton = By.id("iwk");
    private final By searchBtn = By.id("ixj");
    private final By searchInput = By.xpath("//*[@text='搜索']");
    private final By searchResultList = By.id("e4x");
    private final By nameLocator = By.id("ic4");
    private final By departLocator = By.id("bq_");
    private final By editMemberBtn = By.xpath("//*[@text='编辑成员']");
    private final By deleteMemberBtn =  By.xpath("//*[@text='删除成员']");
    private final By confirmBtn = By.xpath("//*[@text='确定']");
    private final By searchResultContent = By.id("hin");
    private final By bottomSelector = By.id("f0d");
    // 右上角三点按钮
    private final By otherBtn = By.id("ix5");

    public ContactPage(AppiumDriver driver) {
        super(driver);
    }

    /**
     * 添加成员
     * @param name 成员名
     * @param phone 成员手机号
     * @return 通讯录页面
     */
    public ContactPage addMember(String name,String phone){
        intoAddMemberPage().addMember(name,phone).back();
        return this;
    }

    /**
     * 进入手动添加成员页面
     */
    public AddMemberPage intoAddMemberPage(){
        swipeDownUntilEleVisible(addMemberBtn).click().click(addManuallyBtn);
        return new AddMemberPage(driver);
    }

    /**
     * 点击返回按键
     */
    public ContactPage back(){
        click(backButton);
        return this;
    }

    /**
     * 搜索并获取member详情
     * @param name 用户名
     * @return 用户数据
     */
    public Member searchAndGetMemberMsg(String name){
        BasePage page = searchMember(name).click(searchResultList);
        Member member = new Member();
        member.setName(page.getElementText(nameLocator));
        member.setDepart(page.getElementText(departLocator));
        return member;
    }

    /**
     * 搜索成员
     * @param name 成员名称
     */
    public ContactPage searchMember(String name){
        click(searchBtn).sendDate(searchInput, name);
        return this;
    }

    /**
     * 编辑成员
     * @param oldName 成员原名字
     * @param newName 新名字
     * @param newPhone 新号码
     * @return
     */
    public ContactPage editMember(String oldName,String newName,String newPhone){
        return intoEditMemberPage(oldName).editMember(newName,newPhone).back();
    }

    /**
     * 删除成员
     * @param name 成员名称
     * @return 删除后的搜索结果
     */
    public String deleteMember(String name){

        try {
            intoEditMemberPage(name).deleteMember();
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return getElementText(searchResultContent);
    }

    /**
     * 进入编辑成员页面
     * @param name 成员名称
     * @return
     */
    public EditMemberPage intoEditMemberPage(String name){
        searchMember(name)
                .click(searchResultList)
                .click(otherBtn)
                .click(editMemberBtn);
        return new EditMemberPage(driver);
    }

    /**
     * 清除成员测试数据
     * @param memberName 测试成员名称
     */
    public ContactPage clearMemberDate(String memberName){
        ContactPage contactPage = searchMember(memberName);
        while (true){
            try {
                List<WebElement> elements = contactPage.findElements(searchResultList);
                elements.get(0).click();
                click(otherBtn).click(editMemberBtn).click(deleteMemberBtn).click(confirmBtn);
                Thread.sleep(2000);
            }catch (Exception e){
                System.out.println("清除完毕");
                break;
            }
        }
        backToContactPage();
        return this;
    }

    /**
     * 返回到通讯录页面
     */
    public ContactPage backToContactPage(){
        try {
            findEle(bottomSelector);
        }catch (Exception e){
            back();
            backToContactPage();
        }
        click(contactBtn);
        return this;
    }

}
